import 'package:easy_chat_app/commons/GlobalStatus.dart';
import 'package:easy_chat_app/commons/db/DBUtils.dart';
import 'package:easy_chat_app/commons/utils/RouteUtils.dart';
import 'package:easy_chat_app/commons/utils/TokenUtils.dart';
import 'package:easy_chat_app/commons/ws/AppWSClient.dart';
import 'package:flutter/material.dart';
import 'package:logger/logger.dart';
import 'package:provider/provider.dart';

class LaunchScreenPage extends StatefulWidget {
  const LaunchScreenPage({super.key});

  @override
  State<StatefulWidget> createState() {
    return _LaunchScreenPage();
  }
}

class _LaunchScreenPage extends State<LaunchScreenPage> {
  Logger logger = Logger();

  @override
  void initState() {
    super.initState();
  }

  Future<void> asyncInitState(
      AppUserDetailChangeNotifier appUserDetailChangeNotifier) async {
    // 数据库初始化
    await DBUtils.initDB();

    // token初始化
    await TokenUtils.loadAndCheckToken();

    await WSWebsocketClient.instance.initWSConnect();

    await appUserDetailChangeNotifier.reload();
    RouteUtils.route2MobileHomePageAndCleanStack();
  }

  @override
  Widget build(BuildContext context) {
    return Consumer<AppUserDetailChangeNotifier>(
      builder: (context, appUserDetailChangeNotifier, child) {
        asyncInitState(appUserDetailChangeNotifier);
        return child!;
      },
      child: const Scaffold(
        body: Center(
          child: FlutterLogo(
            size: 100,
          ),
        ),
      ),
    );
  }
}
